package com.sjy.project.fym.apply.service.impl;

import java.util.Date;
import java.util.List;

import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sjy.project.fym.apply.mapper.FyGoodsApplyHisMapper;
import com.sjy.project.fym.apply.mapper.FyGoodsApplyMapper;
import com.sjy.project.fym.apply.mapper.FyGoodsMapper;
import com.sjy.project.fym.apply.domain.FyGoods;
import com.sjy.project.fym.apply.domain.FyGoodsApply;
import com.sjy.project.fym.apply.domain.FyGoodsApplyHis;
import com.sjy.project.fym.apply.service.IFyGoodsApplyService;
import com.sjy.project.fym.place.domain.FyPlace;
import com.sjy.project.fym.place.mapper.FyPlaceMapper;
import com.sjy.project.fym.user.domain.FyUser;
import com.sjy.project.fym.user.mapper.FyUserMapper;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import com.sjy.common.utils.text.Convert;

/**
 * 防疫物资申请Service业务层处理
 * 
 * @author ljb
 * @date 2022-04-21
 */
@Service
public class FyGoodsApplyServiceImpl implements IFyGoodsApplyService 
{
    @Autowired
    private FyGoodsApplyMapper fyGoodsApplyMapper;
    @Autowired
    private FyGoodsMapper fyGoodsMapper;
    @Autowired
    private FyGoodsApplyHisMapper hisMapper;
    @Autowired
    private FyUserMapper fyUserMapper;
    @Autowired
    private FyPlaceMapper fyPlaceMapper;

    /**
     * 查询防疫物资申请
     * 
     * @param applyId 防疫物资申请主键
     * @return 防疫物资申请
     */
    @Override
    public FyGoodsApply selectFyGoodsApplyByApplyId(Long applyId)
    {
        return fyGoodsApplyMapper.selectFyGoodsApplyByApplyId(applyId);
    }

    /**
     * 查询防疫物资申请列表
     * 
     * @param fyGoodsApply 防疫物资申请
     * @return 防疫物资申请
     */
    @Override
    public List<FyGoodsApply> selectFyGoodsApplyList(FyGoodsApply fyGoodsApply)
    {
        return fyGoodsApplyMapper.selectFyGoodsApplyList(fyGoodsApply);
    }

    /**
     * 新增防疫物资申请
     * 
     * @param fyGoodsApply 防疫物资申请
     * @return 结果
     */
    @Override
    public int insertFyGoodsApply(FyGoodsApply fyGoodsApply)
    {
        fyGoodsApply.setCreateTime(DateUtils.getNowDate());
        return fyGoodsApplyMapper.insertFyGoodsApply(fyGoodsApply);
    }

    /**
     * 修改防疫物资申请
     * 
     * @param fyGoodsApply 防疫物资申请
     * @return 结果
     */
    @Override
    public int updateFyGoodsApply(FyGoodsApply fyGoodsApply)
    {
        fyGoodsApply.setUpdateTime(DateUtils.getNowDate());
        return fyGoodsApplyMapper.updateFyGoodsApply(fyGoodsApply);
    }

    /**
     * 批量删除防疫物资申请
     * 
     * @param applyIds 需要删除的防疫物资申请主键
     * @return 结果
     */
    @Override
    public int deleteFyGoodsApplyByApplyIds(String applyIds)
    {
        return fyGoodsApplyMapper.deleteFyGoodsApplyByApplyIds(Convert.toStrArray(applyIds));
    }

    /**
     * 删除防疫物资申请信息
     * 
     * @param applyId 防疫物资申请主键
     * @return 结果
     */
    @Override
    public int deleteFyGoodsApplyByApplyId(Long applyId)
    {
        return fyGoodsApplyMapper.deleteFyGoodsApplyByApplyId(applyId);
    }
    

    /**
     * 申报物资第一步操作
     * @param fyGoods
     * @return
     */
    @Transactional
    public boolean firstApply(SecuryUserDetail user,List<FyGoods> fyGoodsList) {
    	FyUser fyUser = fyUserMapper.selectFyUserByUserId(user.getUserId());
    	FyGoodsApply apply = new FyGoodsApply();
    	String format = DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
	    String numbers = RandomUtil.randomNumbers(5);
	    String liushui = format+numbers;
    	//判断一下fyGoods格式是否正确
	    
	    //获取当前时段用户打卡所在的place
	    FyPlace fyPlace = fyPlaceMapper.selectFyPlaceBySigninUserId(user.getUserId());
	    //插入apply
    	apply.setLiushuiNumber(liushui);
    	apply.setApplyStatus("1");
    	apply.setApplyNode("待受理");
    	apply.setCreateBy(user.getUserId()+"");
    	apply.setCreateTime(new Date());
    	//apply.setFyGoods(fyGoods);
    	apply.setGoodsContent(formatFyGoodsStr(fyGoodsList));
    	apply.setNextPerson("管理员");
    	apply.setPlaceName(fyPlace.getPlaceName());
    	apply.setPlaceId(fyPlace.getPlaceId());
    	apply.setTeamId(fyUser.getFyTeam().getTeamId());
    	apply.setTeamName(fyUser.getFyTeam().getTeamName());
    	if(fyGoodsApplyMapper.insertFyGoodsApply(apply)>0) {
    		//插入对应的goods
    		for(FyGoods fyGoods:fyGoodsList) {
    			fyGoods.setApplyId(apply.getApplyId());
        		fyGoodsMapper.insertFyGoods(fyGoods);
    		}
    		//插入审批记录
    		FyGoodsApplyHis fyGoodsApplyHis = new FyGoodsApplyHis();
    		fyGoodsApplyHis.setApplyId(apply.getApplyId());
    		fyGoodsApplyHis.setLiushuiNumber(apply.getLiushuiNumber());
    		fyGoodsApplyHis.setApplyNode(apply.getApplyNode());
    		fyGoodsApplyHis.setApplyStatus(apply.getApplyStatus());
    		fyGoodsApplyHis.setRemark(apply.getRemark());
    		fyGoodsApplyHis.setCreateByName(user.getUserName());
    		fyGoodsApplyHis.setCreateBy(user.getUserId()+"");
    		fyGoodsApplyHis.setCreateTime(new Date());
    		
    		hisMapper.insertFyGoodsApplyHis(fyGoodsApplyHis);
    	}else {
    		return false;
    	}
    	
    	return true;
    }
    
    /**返回防疫物资清单具体内容字符串拼接结果*/
    private String formatFyGoodsStr(List<FyGoods> fyGoodsList) {
    	StringBuffer sb = new StringBuffer();
    	for(FyGoods fyGoods:fyGoodsList) {
    		if(!"其他".equals(fyGoods.getGoodsType())) {
    			if(fyGoods.getGoodsType().contains("是否")) {
    				//是否消杀、、是否医废处理
        			sb.append(fyGoods.getGoodsType()+":"+(fyGoods.getGoodsFlag()==1?"是":"否")+";");
    			}else {
        			sb.append(fyGoods.getGoodsType()+":"+fyGoods.getGoodsNum()+";");
    			}
    		}else {
    			sb.append(fyGoods.getGoodsType()+":"+fyGoods.getRemark());
    		}
    	}
    	return sb.toString();
    }
}
